﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Pahan.McuFusion.UI.RecentFiles
{
    public sealed class RecentFile : IEquatable<RecentFile>, IXmlSerializable
    {
        public RecentFile()
        {
        }
        
        public RecentFile(String path)
        {
            this.Path = path;
        }

        public String Path
        {
            get;
            set;
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj as RecentFile);
        }

        public override int GetHashCode()
        {
            return pathComparer.GetHashCode(Path);
        }

        #region Implementation of IEquatable<RecentFile>

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the other parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(RecentFile other)
        {
            if (other == null)
            {
                return false;
            }

            return pathComparer.Equals(this.Path, other.Path);
        }

        #endregion

        #region Implementation of IXmlSerializable

        /// <summary>
        /// This property is reserved, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"></see> to the class instead. 
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"></see> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"></see> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"></see> method.
        /// </returns>
        public XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"></see> stream from which the object is deserialized. </param>
        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement(rootElementName);
            reader.ReadStartElement(pathElementName);
            this.Path = reader.ReadString();
            reader.ReadEndElement();
            reader.ReadEndElement();
        }

        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"></see> stream to which the object is serialized. </param>
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement(pathElementName);
            writer.WriteString(this.Path);
            writer.WriteEndElement();
        }

        #endregion

        private const String rootElementName = "RecentFile";
        private const String pathElementName = "Path";

        private static readonly bool IsUnix =
            Environment.OSVersion.Platform == PlatformID.Unix ||
            Environment.OSVersion.Platform == PlatformID.MacOSX;

        private static readonly StringComparer pathComparer = IsUnix ?
            StringComparer.InvariantCulture :
            StringComparer.InvariantCultureIgnoreCase;
            
    }
}
